//
//  SDRefreshStart.m
//  下拉刷新上拉加载
//
//  Created by dlios on 15-3-5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SDRefreshStart.h"
#import "SDRefreshFooterView.h"
#import "SDRefreshHeaderView.h"

@interface SDRefreshStart ()

@property (nonatomic, strong) SDRefreshFooterView *footView;

@end

@implementation SDRefreshStart

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}


+ (void)SDRefeshTableView:(id)tab delegate:(id<SDRefreshStartDelegate>)delegate
{
    SDRefreshStart *refresh = [[SDRefreshStart alloc] init];
    refresh.delegate = delegate;
    [refresh setupHeaderTableView:tab];
    [refresh setupFooterTableView:tab];
}



- (void)setupHeaderTableView:(UITableView *)tab
{
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    [refreshHeader addToScrollView:tab];
    
    SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    refreshHeader.isEffectedByNavigationController = NO;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            // 操作
            if ([self.delegate respondsToSelector:@selector(headRefesh)]) {
                [self.delegate headRefesh];
            }
            
            [tab reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    // 进入页面自动加载一次数据
//    [refreshHeader beginRefreshing];
}

- (void)setupFooterTableView:(UITableView *)tab
{
    self.footView = [SDRefreshFooterView refreshView];
    [self.footView addToScrollView:tab];
    [self.footView addTarget:self refreshAction:@selector(footerRefresh)];
}
- (void)footerRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UITableView *tab = (UITableView *)[self viewWithTag:100];
        [tab reloadData];
        [self.footView endRefreshing];
        // 操作
        if ([self.delegate respondsToSelector:@selector(footRefesh)]) {
            [self.delegate footRefesh];
        }
    });
}



@end
